#!/usr/bin/python3
# -* - coding: UTF-8 -* -
import jsonpath

from utils import SetResult, TimeSelf, LogSys


def assert_value_by_jsonpath(response, key, value):
    '''
    校验值是否匹配
    接受jsonpath格式的keyout
    :param response: 接口请求返回的json数据
    :param key: jsonpath 定位公式
    :param value: 预期的值，包含关系
    :return:
    '''
    res = jsonpath.jsonpath(response, key)
    if not res:
        SetResult.set_fail('未匹配到数据，Key:{0},实际:{1}'.format(key, response))
        return
    if value in res:
        SetResult.set_pass()
        return
    SetResult.set_fail('预期结果不匹配，Key:{0}，value:{1},实际:{2}'.format(key, value, response))

def assert_key_not_in_json(response, key):
    if key in response:
        SetResult.set_fail('匹配到预期外key，Key:{}，value:{}'.format(key, response))
        return
    SetResult.set_pass()

def assert_no_value_by_jsonpath(response, key, value):
    '''
    校验值是否匹配
    接受jsonpath格式的keyout
    :param response: 接口请求返回的json数据
    :param key: jsonpath 定位公式
    :param value: 预期的值，包含关系
    :return:
    '''
    res = jsonpath.jsonpath(response, key)
    if not res:
        SetResult.set_fail('未匹配到数据，Key:{0},实际:{1}'.format(key, response))
        return
    if value in res:
        SetResult.set_fail('结果中匹配到不该匹配的数据:{}'.format(value))
        return


def assert_value_by_nomal(response, keys, value):
    '''
    校验值是否匹配
    正常匹配，KeyList
    如['key1','key2','']  定位时依次定位
    如需要定位列表，key按照如下格式定义 $0 ,$1
    :param response:
    :param keys:
    :param value:
    :return:
    '''
    all = response
    for key in keys:
        if '$' in key:
            try:
                intKey = int(key.replace('$', ''))
                item = response[intKey]
                response = item
                continue
            except ValueError as e:
                SetResult.set_fail('参数错误:{}'.format(keys))
            except KeyError as e:
                SetResult.set_fail('数据类型与预期不匹配，接口返回：{}'.format(all))
                return  # 参数错误
        try:
            item = response[key]
            response = item
        except KeyError as e:
            SetResult.set_fail('数据类型与预期不匹配，接口返回：{}'.format(all))
            return
    if value == response:
        SetResult.set_pass()
        return
    SetResult.set_fail('预期结果不匹配，Key:{0}，value:{1},res:{2}'.format(keys, value, all))


def assert_type_by_jsonpath(response, key, typeObject):
    '''
    校验值的类型是否匹配
    接受jsonpath格式的keyout
    :param response:
    :param key:
    :param type:
    :return:
    '''
    res = jsonpath.jsonpath(response, key)
    if not res:
        SetResult.set_fail('未匹配到数据，Key:{0},实际:{1}'.format(key, response))
        return  # 为匹配到Key
    if isinstance(typeObject, type(res[0])):
        SetResult.set_pass()
        return
    SetResult.set_fail('数据类型与预期不匹配，预期：{2}，Key:{0},实际:{1}'.format(key, response, type(typeObject)))
    return  # 数据类型匹配错误


def assert_type_by_nomal(response, keys, typeObject):
    '''
    校验值的类型是否匹配
    正常匹配，KeyList
    如['key1','key2','']  定位时依次定位
    如需要定位列表，key按照如下格式定义 $0 ,$1
    :param response:
    :param keys:
    :param Value:
    :return:
    '''
    source = response
    for key in keys:
        if '$' in key:
            try:
                intKey = int(key.replace('$', ''))
                item = response[intKey]
                response = item
                continue
            except ValueError as e:
                SetResult.set_fail('参数错误:{}'.format(keys))
                return  # 参数错误
            except KeyError as e:
                SetResult.set_fail('数据类型与预期不匹配，接口返回：{}'.format(source))
                return  # 参数错误
        try:
            item = response[key]
            response = item
        except KeyError as e:
            SetResult.set_fail('数据类型与预期不匹配，接口返回：{}'.format(source))
            return

    if isinstance(typeObject, type(response)):
        return True
    SetResult.set_fail('数据类型与预期不匹配，预期：{2}，Key:{0},实际:{1}'.format(key, response, type(typeObject)))


if __name__ == '__main__':
    pass